﻿using Biz.Core.Models;
using Biz.Core.Providers;
using Biz.Data.Entities;
using MiniFox;
using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Biz.Data
{
    [AutoResolve]
    public class OrderDataProvider : EntityDataProvider<SampleDbContext>, IOrderDataProvider
    {
        public ResultObject<IEnumerable<OrderVo>> QueryOrderList(PagedQueryObject<OrderSearchModel> pagedQuery)
        {
            // 简单的单表分页查询，实体对象会通过泛型自动查找
            var result = this.PagedQueryExecute<OrderInfo>(pagedQuery);
            return result.GetResultObject(o => new OrderVo
            {
                OrderId = o.OrderId,
                Buyer = o.Buyer,
                Seller = o.Seller,
                PayMode = o.PayMode,
                TotalAmt = o.TotalAmount,
                TradeAmt = o.TradeAmount,
                Paid = o.Paid,
                Items = (from i in o.OrderItemInfo
                         select new OrderItemVo
                         {
                             GoodsName = i.GoodsName,
                             Ordinal = i.Ordinal,
                             Quantity = i.Quantity,
                             UnitPrice = i.UnitPrice,
                             Unit = i.Unit,
                             TaxRate = i.TaxRate
                         }).ToList()
            });


        }

        public bool CreateOrder(OrderDto orderDto)
        {
            var order = new OrderInfo
            {
                OrderId = orderDto.OrderId,
                Buyer = orderDto.Buyer,
                Seller = orderDto.Seller,
                Paid = false,
                TotalAmount = orderDto.TotalAmt,
                TradeAmount = orderDto.TradeAmt,
                PayMode = orderDto.PayMode,
                OrderItemInfo = orderDto.Items.Select((item, i) =>
                                 new OrderItemInfo
                                 {
                                     OrderId = orderDto.OrderId,
                                     GoodsName = item.GoodsName,
                                     Quantity = item.Quantity,
                                     TradeAmount = item.TradeAmt,
                                     TaxRate = item.TaxRate,
                                     UnitPrice = item.UnitPrice,
                                     Unit = item.Unit,
                                     Ordinal = i + 1
                                 }).ToArray()
            };
            return this.Insert(order) > 0;
        }

        public bool UpdateOrder(OrderDto orderDto)
        {
            var orderItems = (from item in this.CurrentDbContext.OrderItemInfo where item.OrderId == orderDto.OrderId select item).ToArray();
            this.Delete(orderItems);

            var order = this.CurrentDbContext.OrderInfo.SingleOrDefault(o => o.OrderId == orderDto.OrderId);
            if (order == null)
                return false;

            order.Seller = orderDto.Seller;
            order.Buyer = orderDto.Buyer;
            order.TotalAmount = orderDto.TotalAmt;
            order.TradeAmount = orderDto.TradeAmt;
            order.PayMode = orderDto.PayMode;
            order.OrderItemInfo = orderDto.Items.Select((item, i) =>
                                 new OrderItemInfo
                                 {
                                     OrderId = orderDto.OrderId,
                                     GoodsName = item.GoodsName,
                                     Quantity = item.Quantity,
                                     TradeAmount = item.TradeAmt,
                                     TaxRate = item.TaxRate,
                                     UnitPrice = item.UnitPrice,
                                     Unit = item.Unit,
                                     Ordinal = i + 1
                                 }).ToArray();
            return this.Update(order) > 0;
        }

        public bool DeleteOrder(string orderId)
        {
            var order = this.CurrentDbContext.OrderInfo.SingleOrDefault(o => o.OrderId == orderId);
            if (order == null)
                return false;
            return this.Delete(order) > 0;
        }

        public OrderVo GetOrder(string orderId)
        {
            var order = this.CurrentDbContext.OrderInfo.SingleOrDefault(o => o.OrderId == orderId);
            if (order == null)
                return null;

            return new OrderVo
            {
                OrderId = order.OrderId,
                Buyer = order.Buyer,
                Seller = order.Seller,
                TotalAmt = order.TotalAmount,
                TradeAmt = order.TradeAmount,
                PayMode = order.PayMode,
                Paid = order.Paid,
                Items = (from item in this.CurrentDbContext.OrderItemInfo where item.OrderId == orderId //order.OrderItemInfo 这个导航在读取时不起作用
                         orderby item.Ordinal
                         select
                           new OrderItemVo
                           {
                               GoodsName = item.GoodsName,
                               Quantity = item.Quantity,
                               TaxRate = item.TaxRate,
                               UnitPrice = item.UnitPrice,
                               Unit = item.Unit,
                               Ordinal = item.Ordinal
                           }).ToList()
            };
        }

        public bool OrderPay(string orderId)
        {
            var order = this.CurrentDbContext.OrderInfo.SingleOrDefault(o => o.OrderId == orderId);
            if (order == null)
                return false;

            order.Paid = true;
            order.TradeTime = DateTime.Now;
            return this.Update(order) > 0; 
        }
    }
}
